/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.ro.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.biz.ResourceURIReplacer;
import com.us.usws.cache.BaseDictCache;
import com.us.usws.cache.UserCache;
import com.us.usws.common.SiteConstants;
import com.us.usws.dao.BaseVideoCatDAO;
import com.us.usws.dao.DeptDAO;
import com.us.usws.dao.VideoAttendeeDAO;
import com.us.usws.dao.VideoCommentDAO;
import com.us.usws.dao.VideoDAO;
import com.us.usws.dao.VideoExtattrDAO;
import com.us.usws.dao.VideoFileDAO;
import com.us.usws.dao.VideoPlayLogDAO;
import com.us.usws.model.domain.base.BaseVideoCat;
import com.us.usws.model.domain.system.Dept;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.channel.DOChannelVideoLink;
import com.us.usws.model.flat.video.DOVideo;
import com.us.usws.service.ro.VideoROService;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.DataPageUtil;

/**
 * Service RO impl class
 * @author robot
 */
public class VideoROServiceImpl implements VideoROService {

	/**
	 * DAO instance
	 */
	private VideoDAO vDao = new VideoDAO();
	private VideoFileDAO fDao = new VideoFileDAO();
	private BaseVideoCatDAO bcDao = new BaseVideoCatDAO();
	private VideoExtattrDAO eaDao = new VideoExtattrDAO();
	private VideoAttendeeDAO atDao = new VideoAttendeeDAO();
	private VideoPlayLogDAO logDao = new VideoPlayLogDAO();
	private VideoCommentDAO cDao = new VideoCommentDAO();
	private DeptDAO deptDao = new DeptDAO();

	/**
	 * Get Count.
	 */
	@Override
	public int getTotalCount() {
		return vDao.getTotalCount();
	}

	/**
	 * Get all list.
	 */
	@Override
	public List<Video> getAllVideos() {
		return vDao.getAllVideos();
	}

	/**
	 * Get all list by pagination.
	 */
	@Override
	public List<Video> getVideos(int start, int end) {
		List<Video> videos = vDao.getVideos(start, end);
		loadVideoProps(videos);
		return videos;
	}

	private void loadVideoProps(List<Video> videos) {
		if (videos != null && videos.size() > 0) {
			SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
			formatter.setTimeZone(TimeZone.getTimeZone("GMT+0:00"));
			BaseDictCache dc = CacheManager.getDictCache();
			UserCache uc = CacheManager.getUserCache();
			for (Video v : videos) {
				v.setVideoTypeName(dc.getDictNameBySepId(SiteConstants.DICT_DOMAIN_VIDEO_TYPE, v.getTypeSid()));
				v.setAuthStatus(dc.getDictNameBySepId(SiteConstants.DICT_DOMAIN_VIDEO_AUTH_STATUS, v.getAuthStatusDsid()));
				User sysUser = uc.getUser(v.getCreateByUid());
				v.setCreateUserName(sysUser.getFullName());
				Dept dept = deptDao.getDept(sysUser.getDeptUid());
				if (dept != null) {
					v.setRole(dept.getName());
				}

				//get categories
				List<BaseVideoCat> cats = bcDao.getCatsByVideo(v.getUid());
				if (cats != null && cats.size() > 0) {
					String[] catUids = new String[cats.size()];
					int i = 0;
					for (BaseVideoCat cat : cats) {
						catUids[i] = cat.getUid();
						i++;
					}
					v.setCatUids(catUids);
				}

				VideoFile videoFile = fDao.getVideoFile(v.getFileUid());
				if (videoFile != null) {
					v.setPlayLen(formatter.format(videoFile.getPlayLen()));
					v.setVideoSnapshotURL(ResourceURIReplacer.getVideoSnapshotURL(videoFile.getUid(), videoFile.getSnapshot()));
					v.setVideoFileURL(ResourceURIReplacer.getVideoFileURL(videoFile.getUid(), videoFile.getFileExt()));
				}
			}
		}
	}

	public void loadVideosByPage(DataPageUtil<Video> dataPageUtil) {
		if (dataPageUtil != null) {
			vDao.loadVideosByPage(dataPageUtil);
			loadVideoProps(dataPageUtil.getResult());
		}
	}

	public void loadVideosAbleAddByPage(DataPageUtil<Video> dataPageUtil, String channelUid) {
		if (dataPageUtil != null && StringUtils.isNotBlank(channelUid)) {
			vDao.loadVideosAbleAddByPage(dataPageUtil, channelUid);
			loadVideoProps(dataPageUtil.getResult());
		}
	}

	public void loadChannelVideosByPage(DataPageUtil<Video> dataPageUtil, String channelUid) {
		if (dataPageUtil != null && StringUtils.isNotBlank(channelUid)) {
			vDao.loadChannelVideosByPage(dataPageUtil, channelUid);
			List<Video> videos = dataPageUtil.getResult();
			loadVideoProps(videos);

			for (Video v : videos) {
				DOChannelVideoLink cvl = vDao.getDOChannelVideoLink(v.getUid(), channelUid);
				if (cvl != null) {
					v.setSeqNum(cvl.getSeqNum());
					v.setIsHot(cvl.getIsHot());
				}
			}
		}
	}

	/**
	 * Get Video by primary key.
	 */
	@Override
	public Video getVideo(String uid) {
		Video v = vDao.getVideo(uid);
		loadVideoProps(v);
		return v;
	}

	/**
	 * get all videos in the channelUid specified
	 */
	@Override
	public List<Video> getChannelAllVideos(String channelUid) {
		List<Video> ret = vDao.getVideosByChannel(channelUid);
		for (Video each : ret) {
			loadVideoProps(each);
		}
		return ret;
	}

	/**
	 * get channel videos with pagination
	 */
	@Override
	public List<Video> getChannelVideos(String channelUid, int start, int end) {
		List<Video> ret = vDao.getVideosByChannel(channelUid, start, end);
		for (Video each : ret) {
			loadVideoProps(each);
		}
		return ret;
	}

	@Override
	public int getChannelVideoCount(String channelUid) {
		return vDao.getVideoCountByChannel(channelUid);
	}

	/**
	 * load video props
	 * @param v
	 */
	private void loadVideoProps(Video v) {
		String videoUid = v.getUid();

		//get video file
		v.setVideoFile(fDao.getVideoFile(v.getFileUid()));

		//get categories
		v.setCategories(bcDao.getCatsByVideo(videoUid));

		//ext attr
		v.setExtraAttributes(eaDao.getVideoExtattByVideoUid(videoUid));

		//attendees
		v.setAttendees(atDao.getAttendeesByVideo(videoUid));

		//play count
		int playCnt = logDao.getPlayCountByVideo(videoUid);
		v.setPlayCnt(playCnt);

		//recommends
		List<DOVideo> recommends = vDao.getRecommendVideos(videoUid, SiteConstants.DFT_VIDEO_RECOMMEND_NUM);
		v.setRecommands(recommends);

		//comments
		int goodVote = cDao.getGoodVoteCntByVideo(videoUid);
		v.setVoteBadCnt(goodVote);

		int badVote = cDao.getBadVoteCntByVideo(videoUid);
		v.setVoteBadCnt(badVote);

		//tags
		String tag = v.getTags() == null ? "" : v.getTags();
		String[] sp = StringUtils.split(tag, ",");
		List<String> tl = new ArrayList<>();
		for (String e : sp) {
			tl.add(e);
		}
		v.setTagList(tl);

	}

	@Override
	public List<Video> getThemeVideos(String themeUid) {
		return vDao.getThemeVideos(themeUid);
	}

	@Override
	public int getIpodiumVideosCount(String userUid) {
		//TODO: add school logic
		return vDao.getTotalCount();
	}

	@Override
	public List<Video> getIpodiumVideos(String useUid, int start, int pagesize) {
		// TODO add school logic
		List<Video> ret = vDao.getVideos(start, pagesize);
		loadVideoProps(ret);
		return ret;
	}
}